/**
 * role-mgr.js
 */

define('role-mgr',['jquery','base','liger.messageBox','liger.dialog','liger.all'],function(require, exports, module){
	
	var Base = require("base");
	var $ = require("jquery");
	require("liger.all")($);
	var ctx = Base.common.utils.getContextPath();
	
	var typeEnum = {0:'超级管理员',1:'普通管理员',2:'高级用户',3:'普通用户',4:'过期用户'};//TODO 来自远程服务器
	var $form = $("#form").ligerForm({
		                 inputWidth: 170, labelWidth: 90, space: 40,icon:'role',
		                  fields: [
		                  { name: "id", type: "hidden" },
		                  { display: "角色名称", name: "name", newline: true, type: "text" , group: "角色信息", groupicon: 'role'}, 
		                  { display: "类型", name: "type", newline: false, type: "select" , options:{data:[{text:'超级管理员',id:'0'},
		                    {text:'普通管理员',id:'1'},{text:'高级用户',id:'2'},{text:'普通用户',id:'3'},{text:'过期用户',id:'4'}]} }
		                  ]
						}); 
	
	var $dataGrid = $("#dataGrid").ligerGrid({
		url: ctx + "/role/fetch.do",
		root:'datas',
        columns: [ { display: '主键', hide: true, name:'id', align:'left', width: 120 } ,
        { display: '角色名称', name: 'name', minWidth: 80 },
        { display: '类型', name: 'type', width: 150, align: 'left',render:function (rowData, index, value){
        	rowData.type_real = rowData.type;
        	value = typeEnum[rowData.type];
        	return value;
        }},
        {display:'操作', isAllowHide: false, width: 150, align: 'left', render:function(rowData, index, value){
//        	console.log(rowData);
//        	debugger;
        	return "<a href='javascript:void(0);' name='op' data-id='" + rowData.id + "'>分配权限</a>";
        }}
        ], data: [], pageSize: 20, sortName: 'name',parms:{},
		width : '98%',
		height : '98%',
		checkbox : true,
		rownumbers : false,
		isScroll : true,
		toolbar:{ items: [{ text: '查询', click: query , icon:'search'},{ line:true },
		                  { text: '编辑', click: toEdit , icon:'modify'}, { line:true },
                          { text: '删除', click: del, icon:'delete' }, { line:true },
                          { text: '分配权限', click: dispathAuthority } ]
                      },
        fixedCellHeight: false,
        onAfterShowData:function(data){
        	$("[name='op']").click(function(){
        		var $this = $(this);
        		alert($this.attr('data-id'));
        	});
        }

    });
    $("#pageloading").hide();
    

    /**
     * 编辑
     */
    function toEdit(){
    	
    	var row = $dataGrid.getSelectedRow();
    	var url = ctx + "/role/toEdit.do";
    	if(row && row.id){
    		url += "?id=" + row.id;
    	}
    	$.ligerDialog.open({ url:url, width: 500, height: 440, title:"编辑" }); 
    }  
    
    /**
     * 查询
     */
    function query(){
    	
    	var form = liger.get("form");
    	var $gridManager = $("#dataGrid").ligerGetGridManager();
    	$gridManager.set({ parms:{param:liger.toJSON(form.getData())} });
    	$gridManager.loadData();
    	
    }
    
    /**
     * 
     */
    function del(){
    	 $.ligerDialog.confirm('是否删除所选记录', function (yes) {
    		var rows =  $dataGrid.getSelectedRows();
    		if(!rows.length){
    			$.ligerDialog.message("请选择您要删除的记录！");
    			return;
    		}
    		var ids = '';
    		for(var i = 0 ; i < rows.length; i++){
    			ids += rows[i].id;
    			if(i+1 < rows.length){
    				ids += ',';
    			}
    		}
			var param = {ids:ids};
	        $.ajax({
	    		url: ctx+ '/role/delete.do',
	    		type : 'POST',
	    		dataType : 'json',
	    		data : param,
	    		success : function(backData) {
	    			if(backData){
	    				$.ligerDialog.tip({  title: '提示信息',content:'记录已经删除！'});
	    				$dataGrid.reload();
	    			}
	    		}
	        });
    		});
    }
    

    /**
     * 分发权限
     */
    function dispathAuthority(id){
    	
    	$.ligerDialog.open({
			height : 500,
			url : ctx+'/custom/authority-mgr.jsp',
			width : 900,
			showMax : true,
			showToggle : true,
			showMin : true,
			isResize : true,
			slide : true,
			title:'添加权限'
		}); 
    	
    	
    }

});